/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package educar.models;

import educar.db.JPA;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

/**
 *
 * @author Matias
 */
public class Cursa {
     private static JPA jpa = new JPA();
     private String codMateria;
     private String nroAlumno;
                
	public Cursa(String codMateria, String nroAlumno) {
                this.setCodMateria(codMateria);
		this.setNroAlumno(nroAlumno);
        }
        
	public static boolean authenticate(String codMateria, String nroAlumno) {
		try {
                    Cursa d = Cursa.getCursaByKeys(codMateria, nroAlumno);
                    if ((d.getCodMateria().equals(codMateria)) && (d.getNroAlumno().equals(nroAlumno))){
                        return true;
                    }
			return false;
		} catch (cursaNotFound ex) {
			return false;
		}
	}

	public boolean save() throws cursaNotFound {
		try {
			Cursa.getCursaByKeys(codMateria, nroAlumno);
			System.out.println("encontrado");                        
			return false;
		} catch (cursaNotFound e1) {
			String[] columns = { "codMateria", "nroAlumno" };

			PreparedStatement stm = jpa.newRecord("educar_dev.cursa", columns);

			try {
				stm.setString(1, codMateria);
			        stm.setString(2, nroAlumno);
				
			} catch (SQLException e) {
				System.out.println("error al guardar");
                                e.printStackTrace();
				return false;
			}
			jpa.create(stm);
			return true;
		}
	}

	public void destroy() {
                String[] keys = { "codMateria", "nroAlumno" };
                String[] ids = {this.getCodMateria(), this.getNroAlumno()};
		jpa.destroyByKeys("cursa", keys , ids);
	}
	
        public static void destroyCursaByKeys(String codMateria, String nroAlumno) {
		String[] keys = { "codMateria", "nroAlumno" };
                String[] ids = {codMateria, nroAlumno};
                jpa.destroyByKeys("cursa", keys , ids);
	}
	
        public static Cursa getCursaByKeys(String codMateria, String nroAlumno) throws cursaNotFound {
		ResultSet rs = null;
		Cursa d = null;
                String[] keys = {"codMateria", "nroAlumno"};
                String[] ids = {codMateria, nroAlumno};

		rs = jpa.getByKeys("cursa", keys , ids);

		try {
			if (rs.next()) {
				d = new Cursa(rs.getString(1), rs.getString(2));
			} else {
				throw new cursaNotFound();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

		 return d;
	}
        
        public boolean setCursaByKeys (String oldCODMateria, String oldNROAlumno) {
                String[] keys = {"codMateria", "nroAlumno"};
                String[] ids = {oldCODMateria, oldNROAlumno};
                jpa.updateByKeys("cursa", "CodMateria", codMateria, keys, ids);                
                jpa.updateByKeys("cursa", "nroAlumno", nroAlumno, keys, ids);            
              	return true;
        }

    public void setCodMateria(String codMateria) {
        this.codMateria = codMateria;
    }

    public void setNroAlumno(String nroAlumno) {
        this.nroAlumno = nroAlumno;
    }

    public String getCodMateria() {
        return codMateria;
    }

    public String getNroAlumno() {
        return nroAlumno;
    }
        
    
        
}


